var app = getApp();
// const util = require('../../index/index.js');
var timer1 = null;
var speed = 250;    //发送20bit数据间隔
var largeSpeed = 200;   //发送133bit数据间隔
var errorNum = 0;   //数据发送失败次数

var packageBox = [
    '0101fe0c94a4000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c94c1000c9431040c94c1000c94c1000c94c1000c94c100bc0c210d210dc00cc00cc00cc00cc60c210d210dc90cc90cc90cd10cd10cd10cdf0cea0c210d210d210df50c7c21',
    '0102fdfc0cf50cfc0c030d030d0a0d0a0d210d210d210d210d210d210d210d210d210d210d210d210d210d110d150d190d1d0d7e000e0164006400170014005000e703ff00ff00ff001700e703ff00ff00ff000f27ff00ff00ff00ff00f401e703fc03e703e703ff03f401ff00e803e803e803e803e803e803e803e803e803e803e8030d50',
    '0103fce803e803e803e803e8030f27e703ff000f27ff00090c0909090909093f065b4f666d7d077f6f777c395e79714077786e714f3f796d3f79796f067979070679795e3779795050790011241fbecfe5d8e0debfcdbf10e0a0e6b0e0e6e1f0e302c005900d92ac3cb107d9f713e0acecb0e001c01d92af3bb107e1f70e94e7070c94169d',
    '0104fb09180c940000909a919a0000919800009098089591980000909a0000919a0000909891980895909890e087ff0ac0919a00000000909a0000000000000000000009c0919800000000909a0000000000000000000090989f5f983011f0880fe5cf919a0000000000000000909a000000000000000000009098000000000895919877be',
    '0105fa919a80e090e0909a000000000000880f81998f5f909800000000000000009f5f983089f7899a9098000000000000909a00000000000000009098000008950f931f93182f062f0e94c300812f0e94d300802f0e94d3000e94ca001f910f9108951f930e94c3008fe40e94d3000e94ff00182f0e94ca00812f1f910895ef92ff92e4d4',
    '0106f90f931f93e82e162f042ff22e0e94c30088e40e94d3001e29102b812f8f290e94d3000e94ca001f910f91ff90ef900895ff920f931f93982f162f042ff22e88e6692f0e941f018ae6612f0e941f018ce6602f0e941f018ee66f2d0e941f011f910f91ff900895cf92df92ff920f931f93cf93df93f82e192fe091e402f0e0e45d3fa4',
    '0107f8fe4fe491e093bb03362fc42edd2417c08f2d912f602f70e00e94cd17e82ff0e0e45efe4fe491c554dc4fe8838f2d912f602f70e00e94cd17f62e172f3f5fc32fd0e0cc16dd0624f7222339f0e22ff0e0e654fc4f808180588083df91cf911f910f91ff90df90cf900895e82ff0e0ee0fff1fee0fff1fe05dfe4fabebb3e08491c736',
    '0108f78d93319683e0af3bb807c9f70895a3b3f091700053b340917100e09174006091750083b390e00090720002c0959587950a94e2f780ff36c021e030e0c90102c0880f991f4a95e2f785230e94c117482f02c0220f331ffa95e2f78a2f82236e2f0e94c117480f80917f02882349f0842f8150823028f49ee0941b10927f0201c00c17',
    '0109f690e1433041f48091ed02882331f081e080937f0202c01092ed0210922501109224014ac08091240190912501449710f490e142c08091240190912501873a9105d0f180910501882331f18091800290918102892b01f1809124019091250189529a4048f18de89ae0909325018093240180912c0390912d038e50914031f481e00a77',
    '010af5809301011092020116c081e0809302011092010110c08091240190912501815a9f4048f089e893e1909325018093240181e0809302019fe0892f089586e991e0089580914601882331f48091190390911a03892b29f08091b0029091b10205c08091b0029091b102829790937c0280937b0280917b0290917c0280937d029093ca8a',
    '010bf47e020895873148f4e0e0f0e028e730e040e050e066e170e029c0883648f4e8e7f0e02ae930e046e150e067e670e01ec08e3b48f4eae9f0e028ed30e047e650e06deb70e013c0873f48f0eaeff0e02aef30e046ef50e06fef70e008c0e8edf0e02aef30e04deb50e066ef70e02e1b3f0b90e0841b950b641b750bac01249fc0018a5d',
    '010cf3259f900d349f900d11240e94cd17862f8e0f0895809163008f70806487b980e888b98dec86b9789408951f932091d7003091d80036952795369527951091ea01e12ff0e08091790290917a02820f931fef5afd4f4081841b910990937a0280937902208368e270e00e94cd177093f9006093f8001f5f1093ea01183238f110925f0b',
    '010df2ea013091e901e32ff0e0e551fe4f208180914f0290915002821b9109860f971f9093500280934f026083880f991f6de270e00e94cd177093fd006093fc003f5f3093e9013a3510f01092e9011f9108951f932091d3003091d40036952795369527951091a001e12ff0e08091e7019091e801820f931fef55fe4f4081841b910900a0',
    '010ef19093e8018093e7012083880f991f63e270e00e94cd177093f7006093f6001f5f1093a001163410f01092a0011f91089580913603882311f01fb80ec08091c800882319f08091c90006c0e0917403f0e0e858ff4f80818fb9779bfecf9fb18091c800882369f1953519f0963569f402c086e50bc08091c900863519f41092c8008dd7',
    '010ff006c09093c90003c085e58093c900809174038f5f80937403809174038c3328f0109274031092f3023dc081e28093f3028091c800882309f03fc010927403089580917403882361f49337a1f4e0917403f0e0e85cfc4f908381e0809376030ac080917603882331f0e0917403f0e0e85cfc4f9083809174038f5f80937403809145d2',
    '0110ef74038c33b0f0109274031092f30291e09093750380917603882321f0909337031092760381e08093c80085e58093c900089581e28093f302089540911b0150911c0130918301e32ff0e080919e0190919f01840f951fec57fe4f2081821b910990939f0180939e014083880f991f6de070e00e94cd177093fb006093fa003f5f0d55',
    '0111ee309383013a3110f01092830108954091350130917b01e32ff0e08091810190918201840f911de458fe4f2081821b910990938201809381014083880f991f880f991f65e070e00e94cd177093f5006093f4003f5f30937b01353010f010927b0108951f920f920fb60f9211241f932f933f934f935f936f937f938f939f93af93ed2c',
    '0112edbf93ef93ff93a0917701b0e084b195b1fd01ee0fff1fe959fe4f91838083a854bf4fac91ea2ff0e0ee0fff1fef52ff4f91838083a33021f481e08093340305c0a43019f481e080933503e0916601e0936300f0e0e054ff4f8081809377018f70806487b9369a80917901882341f081e080936601809378011092790115c080918b2e',
    '0113ec7a01882319f41092660104c08f5f8093660181508f5f80937a01873010f010927a0181e080937901809116038f5f8093160380911703909118030196909318038093170380917801882309f442c08091d3009091d4009695879596958795182f80933501809148018f5f80934801853118f084e1809348010e9407048091800208a4',
    '0114eb90918102892b19f1809127038823f9f480911a018823d9f4809105018823b9f080916400882399f4809133038f5f8093330380913b01181710f010933b019091350180913c01891710f090933c01109278018091f302813240f080910601882321f0c4980e945803c49aff91ef91bf91af919f918f917f916f915f914f913f914b78',
    '0115ea2f911f910f900fbe0f901f901895409165014830b0f5413011f04330e9f4e42ff0e0ee0fff1fdf01a054bd4f2d913c911197ef52ff4f808191819695879596958795820f931f3695279536952795821b930b8d939c930dc0e42ff0e0ee0fff1fdf01af52bf4f8d919c91e054fd4f91838083842f8f5f80936501883011f41092c975',
    '0116e96501089581ea8fbd89e08ebd89e683bf85bd1bbc1abc19bc18bc83e28cbf84e683bd8d9a8c9abb9a8f9a089587b3806a87bb8db181658db9709a0895883158f44091a4025091a502e0e0f0e020e030e067e170e01bc0833768f4e091a402f091a5024091a6025091a70227e130e062e770e00cc04091a8025091a902e091a602307c',
    '0117e8f091a70222e730e06fef70e04e1b5f0b90e0821b930b621b730b9c01429fc001439f900d529f900d11240e94cd17862f8e0f089508955091800240e827e030e0852f642f0e94c117f901e25ffc4f80838081882319f0541708f0541b21503040222311f04695eccf08951f93cf93df930e94c701182f803108f022c28091cb005084',
    '0118e7883009f020c21f3009f04ec180910701882341f1809108018f5f8093080180910801883008f440c11092080180910b01882309f439c18fe190e060e070e00e94f4176091a0027091a10281e290e00e94f4176091a2027091a30283e290e00e94f41722c120e030e08091800290918102892b11f021e030e0c901880f991f820fa230',
    '0119e6931f8093fe009091e4029e3198f48091d000882379f0892f90e0880f991ffc01e058fd4f608171818f5f9f4f0e94f417109200018091e4028e3109f075c08091bc029091bd0280359105f9f08135910538f48431910579f0889709f065c00fc08837910511f1883a9105f1f18436910509f05ac012c081e08093070155c081e0bf3c',
    '011ae58093ca001092250305c081e0809325031092ca001092260347c081e0809326031092ca00109225033fc0c0e0d0e0fe01e053fd4f11821082ce018f5a9f4f60e070e00e94f4172296c830d10581f78cef93e09093d9028093d80289e590e06cef73e00e94f41720c02091da023091db0280912f039091300328173907a9f484efef4e',
    '011be491e09093db028093da028be590e064ef71e00e94f41783e690e067e170e00e94f4178ee2809361002091e4022f5f2093e4028091800290918102892be9f0809167008823a1f481e08093e40280912a0390912b03019690932b0380932a03099708f458c010922b0310922a0353c0243009f050c086e04cc08091ca008823b1f0c89d',
    '011ce3263119f487e18093e4028091e402883119f48ae18093e4028091e4028b3119f48de18093e40284e180936200809125038823f9f08091e402843119f486e18093e4028091e402873119f488e18093e4028091e4028a3119f48be18093e4028091e4028d3119f48ee18093e40282e18093620080912603882369f08ee180936200bd98',
    '011de28ce2809361008091e4028f3119f488e28093e4029091e40280916100891720f4809162008093e402812f8c50823008f0c2c06091e40280910701882389f0e0910801f0e0ec5efe4fe4914e2f50e0e0910801f0e0ea5ffc4f8081282f30e012c06f3108f061e0a62fb0e0aa0fbb1f9d0120553f4ff90185919491a058bd4f2d91dc69',
    '011ee13c91ac011c3039f02f5f3f4f4217530738f49a0105c02115310511f0215030408091800290918102892b29f01092ff0081e08093000180910701882309f450c0e0910801f0e0ea5ffc4f208380910801843018f58091060390e064e0880f991f6a95e1f720910703820f911d54e0880f991f5a95e1f720910803820f911d44e0e97c',
    '011fe0880f991f4a95e1f720910903820f911d9093a1028093a0022bc080910a0390e034e0880f991f3a95e1f720910b03820f911d24e0880f991f2a95e1f720910c03820f911d14e0880f991f1a95e1f720910d03820f911d9093a3028093a20208c0e62ff0e0ee0fff1fe058fd4f318320838091800290918102892b79f02091e20225d5',
    '0120df3091e30280918202909183028217930720f4309383022093820286e991e09093e7028093e60280911b030e948202809320031092210381e08093760089e001c088e08093cb00df91cf911f9108952f923f924f925f926f927f928f929f92af92bf92cf92df92ef92ff920f931f93df93cf93cdb7deb761970fb6f894debf0fbe121b',
    '0121decdbfa59aad9aba9ac29ab89ac098bc9ac49a81b3836081bb909800e010e0c8018f5f9f4f0e94e117f801e058fd4f918380830e5f1f4f0436110589f788e290e09093bd028093bc020e94ca020e9442050e9456050e949b058091aa029091ab02909323038093220382e380936f0082e08093700013e01093710094e090937200f6da',
    '0122dd86e0809373009093740090937500b99ac19880e060e040e021e00e943e0180e060e040e020e00e94580183b390e00090720002c0959587950a94e2f780ff06c01093610081e0809362000bc087e28093610082e18093620010928102109280028de18093e40280e19ee028ec30e0f9013197f1f70197d9f780919e0290919f020af7',
    '0123dc80589d4019f481e080930b011cbe86e991e09093e7028093e6028091c2029091c3020e9461029093e9028093e802209182023091830283e390e0ac01249fc001259f900d349f900d112466e370e00e94cd17862f0e94820280932003109221038ce090e090938902809388025091e5025b8761e06a87698768876f836e836d8318c5',
    '0124dbee24dd24332433946c83ff2400e0cc24552444241b82198a188a66247724aa24bb248ce291e09a838983222411e0e3e3f1e094919c873197a491ad873197b491be87319724912f87809116038730f8f0209149012a30d8f4e22ff0e0df01a651bd4f8c918f5f8c93ee0fff1fe25efe4f80819181019691838083822f8f5f80930c62',
    '0125da49018a3021f41092490110921603809133038333a0f01092330380913b0190e020913c01821b910990931c0180931b010e94dd0310923b013fef30933c0180913403882309f47cc02091d7003091d80080912003909121032817390708f065c08091160190911701892b09f45ec080912403882309f059c08091800290918102a0ee',
    '0126d9892b09f452c080913103909132030196909332038093310336952795369527958091370190e02817390710f0209337018091380190e08217930710f02093380180913103909132038497c0f110923203109231038091370190913801891b80931f038430c0f48091280390912903019690932903809328038232910580f0109396e0',
    '0127d83d018d52914058f08de291e0909329038093280304c01092290310922803109237019fef9093380109c0109229031092280310923701afefa09338011092340380913503882309f445c080911a01882309f040c0809104018823d1f1809124038823a9f080912a0190912b010b9780f18091d9009091da00c39710f010931101c5ef',
    '0128d7efe1f0e0f0932b01e0932a0121c08091d9009091da00069798f480911d0390911e03019690931e0380931d038739910558f02ce231e030931e0320931d0304c010921e0310921d0310922b0110922a011092350380911703909118038f55914038f0002329f0109218031092170300e00e94b5058091800290918102892b09f44799',
    '0129d64cc2409140015091410150933f0140933e012091f4003091f500309341012093400180911e0190911f018159914008f476c180914401909145014217530740f48217930760f4309345012093440107c08417950720f4509345014093440180914601882309f0d2c080911a01882309f0c8c080910501882309f44fc0809164003288',
    '012ad5882341f48091f100882321f4809124038823f1f02220e1f48091b4029091b50244972091f4003091f5002817390710f0cc240ec0c3948ee18c1550f41093460110937600309343012093420144e2c42e2091f4003091f5008091b4029091b50282179307a8f48091510190915201019690935201809351018159914068f181e91c62',
    '012bd491e090935201809351012224239424c010925201109251011fc020914401309145018091b4029091b5022817390710f0cc2412c0c394b2e3bc1570f410934601109376008091f4009091f50090934301809342013ce3c32e8091b4029091b5028c559e4f20914401309145018217930710f0442417c043948091f4009091f500d283',
    '012cd39093450180934401eee1e41558f4c0981092130110934701109265001092d00028e2422e80914701882359f1ad9889e190e09bbd8abd84e683bd8091b4029091b502815b9e4f2091d3003091d4008217930718f41092ef0214c08091ef028f3d80f01092ef02fb81ff5ffb83f43048f01093650010924701c09a1093130125e0b942',
    '012dd22b8310922301109222015cc080912201909123018c38910510f4c0982cc08091220190912301845e924010f4c19a23c080912201909123018057934010f4c09a1ac080912201909123018c5f934028f480912201909123010ec080912201909123018c5f934039f410936400ad988ced83bd81e08c838091b4029091b5028854a235',
    '012ed19e4f20914401309145018217930710f0552418c053948091f4009091f500909345018093440193e0951560f480914601882331f01092460110931a011093760094e0592e80911a01882331f1809162018830e8f084e683bd809182028093cc0010921a011092450110924401109364001092240310920401109311011092f10037a8',
    '012fd08ae080936201a1e0ac83cc2420c013bc1092cc00c1981bc01092620118c080911e0190911f018f55914068f02536310510f0c09a08c0c09810921f0110921e0183e00e94b50110922301109222012091f4003091f5008091d6029091d7028217930720f43093d7022093d6028091200190912101895e9d4f30f088ee9def9093b6d8',
    '0130cf210180932001809120019091210181569a4e50f180916400882321f48091f100882311f110922101109220016091d0027091d1026f5f7f4f7093d1026093d00281e590e00e94f4176091d6027091d70287e590e00e94f4176091d8027091d90289e590e00e94f417809146018823b9f180912201909123018d5f934080f180915423',
    '0131ce220190912301895e9d4f30f088ee9def9093230180932201809122019091230187569e4ee0f080916400882321f48091f1008823a1f086e094e090932301809322016091d2027091d3026f5f7f4f7093d3026093d20283e590e00e94f41780910101882389f0809126019091270188399105d8f0809157019091580101969093b54e',
    '0132cd58018093570104c01092580110925701109227011092260108c080911e0190911f018f55914008f0c09a80913703882309f44dc020e030e0f901e85cfc4f8081f901e958fc4f80832f5f3f4f2c33310599f790917803809179039817b1f5809178038131a9f0823138f4833001f1803161f0813041f525c0833181f0833158f0e16b',
    '0133cc843179f08531f9f40fc0109377001bc01092770018c01093b30317c01093b40314c01093b50311c01093b6030ec0809136038f5f809336036e9885e00e94b50104c08093b703109236031092370380917503882309f489c080914a01e82ff0e0ee32f10508f06dc0e65dff4fee0fff1f0590f491e02d099483e7809378006ac0bb34',
    '0134cbe82ff0e0df01a854bc4f60c0f0927f0061c0e82ff0e0df01aa0fbb1fa255bd4f07c0e82ff0e0df01aa0fbb1fa455bd4f8d919c91969587959695879548c08091d2029091d30296958795969587958093880040c08091da029091db0296958795969587958093890035c0e82ff0e0e858ff4f809182022dc0e82ff0e0e858ff4f876e',
    '0135ca8091830226c0e82ff0e0e858ff4f809184021fc0e82ff0e0e858ff4f8091860218c06f856093a20015c08e858093a30011c09d859093a4000dc0ac85a093a50009c0e82ff0e0df01a958bc4f8c91e858ff4f808380914a018f5f80934a018c3320f010924a011092750380910501882309f46dc080912703882309f068c08091bb80',
    '0136c92403882309f063c080911a01882369f080914601882309f45ac080912201909123018d5f934008f452c080916400882359f0e091ae02f091af02fa83e9831093f10082e890e042c02091c2023091c3024091ba025091bb024217530788f5ca01c8968217930768f428533048241b350bc901880f991f69e070e00e94cd176a5e43fd',
    '0137c810c0241b350b8be090e0ac01249fc001259f900d349f900d11246de170e00e94cd176b3608f06ae62091b8023091b902862f90e08217930738f4822f861b02c08091b80280934e0180914e0183bd8091ea02853108f493c10e9407052091ce023091cf0251e02b39350710f0aa240cc08be991e0821b930b880f991f803b91052d0d',
    '0138c710f08fea90e0a82e2091cc023091cd0261e02c31360710f0bb240ac08ce191e0821b930b8c34910510f08be490e0b82e8091c2029091c3020e9461029093e9028093e80280911301882309f455c180910d01882309f441c18091c2029091c302865b934008f0ccc08091b600882309f4c7c08091ce029091cf028752924040f4f9ea',
    '0139c68091cc029091cd028558934008f4a8c08091800290918102892b09f46fc080916400882341f48091f100882321f480912403882331f180912c0190912d01895b9b4010f4332418c08bea99e290932d0180932c018091c0029091c10223e08f38920710f0c29804c08857934008f4c29ab2e3db2e3324339410922f0110922e01d6c7',
    '013ac57cc010922d0110922c0180916400882309f073c08091f100882309f06ec080911501882309f069c03320f1f080912e0190912f01873a910518f4a2e3da2e1cc080912e0190912f018e54914018f4f4ebdf2e12c080912e0190912f01855f914018f4ecedde2e08c088ee93e090932f0180932e01dd24da94c29a3fc0809164009a0f',
    '013bc4882321f48091f100882329f010922f0110922e0107c080912e0190912f01873a910518f472e3d72e28c080912e0190912f018e54914018f464ebd62e1ec080912e0190912f01855f914018f45cedd52e14c088ee93e090932f0180932e010ac088ee93e090932f0180932e0110922d0110922c01c29add24da941092ee0217c0ab58',
    '013cc38091ee02833430f084e68093ee021093b60002c01092b60088ee93e090932f0180932e0110922d0110922c01dd2480916400882349f48091f100882329f480912403882309f44fc0809148018a3008f44ac020915501309156012115310579f48091c2029091c3029093500180934f0181e090e090935601809355012bc080914d7d',
    '013dc24f01909150014091c2025091c3028417950728f410925601109255011bc0c90101969093560180935501c39758f04c52514040f410935d0188ec90e090935601809355018091c2029091c3029093500180934f011092480106c010922f0110922e0142e3d42e80915d01882319f0efefecbf03c0dcbe01c01cbe1092ea02809150b6',
    '013ec1ec028b3508f4cec3809163018f5f80936301809162018f5f80936201809161018f5f8093610180915f019091600101969093600180935f0180916400882341f48091f100882321f4809124038823e1f14091c2025091c3022091ba023091bb0224173507b8f4421b530b84e190e09c01429fc001439f900d529f900d11246be1e3a3',
    '013fc070e00e94cd178091aa029091ab02861b970b16c0241b350b84e190e0ac01249fc001259f900d349f900d11246be170e00e94cd178091aa029091ab02860f971f90932303809322038091fe008f5f8093fe005b85552329f48091ff008f5f8093ff0080910d01882309f454c040918002509181024115510539f080914b0188238703',
    '0140bf19f488ec90e004c08091e6029091e7022091ce023091cf028217930718f080e090e006c080e02a5f334008f081e081278e83452b39f080914b01882319f428ec30e002c027e131e019868091cc029091cd022817390710f481e089872091ca023091cb028091e8029091e9028217930718f01a861f8203c091e09a879f83809133de',
    '0141bed8029091d9022817390720f43093d9022093d8028091800290918102892b31f410936600a0916600ad83a887bf81bb2311f070e001c072e0e985ee2311f060e001c064e0fe81ff2311f050e001c058e080916600882311f040e001c040e12885222311f030e001c030e28d81882311f020e001c020e480916500882311f090e04570',
    '0142bd01c090e880e0aa85aa2309f481e0f72ef82af62af52af42af32af22af92a80910101882309f1aa2029f4bb2019f4b3e0fb16d1f4809167008823b1f080915701909158019093f5028093f402e0ecf2e0a6efb2e0819191918d939d9322e0e03df207c1f71092670080910201882339f080ec9be290931f0180931e01ff248091fdcb',
    '0143bc1e0190911f018b5a994208f441c01093050180911e0190911f0183599d4238f180912703882319f580911a01882359f0809146018823d9f080912201909123018d5f9340a0f01093060180918802909189026ce070e00e94cd176093040184eb9de290931f0180931e0110930d0110926401ff20a1f0f09264018f2d835086304f61',
    '0144bb58f48be992e090931f0180931e0104c0f0906401ff2019f082e08093fe008091fe00843030f01093d00083e28093fe0002c01092d000809100018823a1f08091ff008b3080f08091e40290e0880f991ffc01e058fd4f608171818f5f9f4f0e94f4171092000180910701882359f18091b500882361f01092b500809108018430015c',
    '0145ba08f472c08091a2029091a30271c01093b50080910801843058f480910801e3e0f0e0e81bf109e554fc4f80e48083f7c180910801e7e0f0e0e81bf109e554fc4f1082edc180917700882321f080913603882339f180913603882361f080e48093bb038093be0383e78093bc038ee38093bd03d5c18091b500882349f01092b50052e7',
    '0146b91092bb031092bc031092bd0365c11093b50080e88093bb038093bc038093bd0358c180916e00882389f080e00e94b5018091b8038f5f8093b803863008f4afc110926e0089e08093b8032dc080912e038823a9f580915e018f5f80935e01823008f49dc1883058f48091a0029091a10260e043e020e000e10e94730190c180910d11',
    '0147b8a2029091a30260e043e020e000e10e94730180915e018d3009f081c18ee080935e0110932e031093d0008091800290918102892b09f073c11092fe0070c18091d000882309f444c18091e402803210f0109376008b2d8a0d21f480910101882319f01093760002c01093b50080917600882309f454c1109276006b85662329f03242',
    '0148b780918002909181021ec1e091e402ef3109f4e5c0e03268f4e43038f4e23008f0abc0e13009f008c117c0e53009f004c1acc0e93209f4f1c0ea3238f4e13209f4e2c0e83209f0f8c0e3c0ea3209f4eac0eb3209f0f1c0ebc0809167008823d9f480912a0390912b03fc01ee0fff1fec50fd4f0190f081e02d892b11f0cf01e3c08771',
    '0149b6cf0161e043e020e00ae00e94730188e78093bb0304c18091b500882309f43fc01092b50080914601882389f08091e402e82ff0e0ee0fff1fe058fd4f80819181813a910520f080ea90e091838083e091e402f0e0ee0fff1fe058fd4f0190f081e02d83e390e0dc01ea9fc001eb9f900dfa9f900d112466e370e00e94cd1770931cd2',
    '014ab51c0360931b03f0932d03e0932c03b1e0ee30fb0710f410920101cf0134c0e093b50080910101882359f08091ce029091cf0260e043e020e00ae00e947301adc0aa2069f0bb2011f080ec0dc01092be031092bd031092bc031092bb039ec0bb2009f49bc080e48093be038093bd038093bc038093bb0391c0f0e0ee0fff1fe0581ee6',
    '014bb4fd4f8081918161e05fc080918a0290918b029c01220f331f220f331f280f391f2436310511f423e630e0c90161e042e023e00ae00e94730182e58093bb0380911003882349f480910f03882309f067c088e18093be0363c01092be0360c0809182029091830223e330e0fc01e29fc001e39f900df29f900d112466e370e00e942e45',
    '014cb3cd17cb0120c08091c2029091c3021bc08091d0029091d102abce8091d2029091d302a6ce8091d4029091d502a1ce8091d6029091d70207c0f0e0ee0fff1fe058fd4f8081918160e043e020e00ae094ce8091800290918102892b19f0fb85ff2369f08091e40290e060e043e020e00ae00e9473011092be031092bd03ff2069f0e73a',
    '014db28f2d90e060e043e020e00ae00e94730189e78093be038093bd03109376001092ec0201e080910501882309f431c280912403882309f02cc280911a01882369f080914601882309f423c280912201909123018d5f934008f41bc280911d0390911e038550914030f0109364001093760041e04c8380911d0390911e03c39798f0bfad',
    '014eb180913d01882379f01092170110921601109219011092180110923d01109364001093110104c05c81513009f0c7c029813a81245630408091d7009091d8008217930720f4809104018823b9f080911101882321f080910401882379f48091d9009091da008d52914008f4d0c18091800290918102892b09f0c9c1109264008091ba53',
    '014fb0f100882321f480911101882321f01092f10010921101109229011092280110921e0310921d0380911403882331f484e190e090938702809386028091100320911b0330911c03882309f04dc02d37310568f430935a01209359018091860290918702880f991f998b888b1cc08de790e090935a018093590180918602909187025c1a',
    '0150af880f991f820f931f8f3f910531f028f0a2e8b0e0b98ba88b05c0fc01ed57f040f98be88b2091860230918702220f331f40911b0350911c03c901840f951f90935c0180935b018f3f910559f050f08fef90e03c01641a750a90935c0180935b0117c0390115c02c33310538f430935a0120935901198a188a0bc08ce390e09093ec21',
    '0151ae5a0180935901a9014c535040598b488be090590180918a0290918b022ee630e0dc01a29fc001a39f900db29f900d112490934d0180934c011c822ac180911003882309f0e1c0209128013091290180914c0190914d018217930708f05cc04091d7005091d80060912203709123036417750718f4e0901b0330c0809114032091ddec',
    '0152ad1b0330911c03fb01e41bf50baf01882359f48ce390e0dc01ea9fc001eb9f900dfa9f900d11240ec08091840290918502880f991ffc01e49fc001e59f900df49f900d11240e94cd17620f731f6f3f710519f010f06fef70e0e62e80916101863058f080e18093610180911d01882321f410931d01109376008091280190912901b31f',
    '0153ac895e934008f443c084e299e090932901809328013cc080912801909129010b97a8f080912801909129010a979c01629ec001639e900d729e900d11246eed70e00e94cd17e0905b01e61a1fc0809128019091290148895989dc014a9fc0014b9f900d5a9f900d11246ae070e00e94cd172091590130915a01260f371f2f3f3105151b',
    '0154ab19f010f02fef30e0e22e109261012091d7003091d80080912003909121032817390710f5809124038823f1f48091800290918102892bc1f08091180190911901019690931901809318010697a8f086e090e0909319018093180181e090e0909317018093160108c01092170110921601109219011092180180913d01882309f478a0',
    '0155aa47c0a8e2ea2e44c08091d7009091d8008436910598f460918c0270918d0220911b0330911c032617370710f4e22e01c0e62e10922901109228012ac0809128019091290160914c0170914d016817790748f4e0901b036f5f7f4f709329016093280116c08091280190912901e889f9899c01e29fc001e39f900df29f900d1124f637',
    '0156a90e94cd17e0905901e60e02c031e03c8320914601222341f080912201909123018d5f934008f471c08091280190912901895e934008f469c080910501882309f464c08091ca029091cb028859934008f05cc0222319f08ae590e002c08ce890e090935401809353012091530130915401c90106964091fa005091fb00841795071bf9',
    '0157a828f58091f1028531e8f18091190390911a03019690931a038093190320911b0330911c032135310550f0205530402817390748f430931a032093190304c010921a03109219031092f1021cc01092f1022a5030404217530740f58091f202853120f18091190390911a038230910530f0019790931a038093190304c010921a035ce6',
    '0158a7109219031092f20210c080916400882341f48091f100882321f480912403882321f010921a03109219038091800290918102892ba9f180910601882389f180910401882321f180916400882321f48091f100882371f080912403882351f4ad9a109324038ced83bd10922b0110922a0117c080912403882399f0809111018823f1f0',
    '0159a679f0ad981092240381e08c8309c080912403882321f01092240310936400ad9880911e0190911f018b59924008f4bac080914701882311f4109313018091b700882341f08de598e290931f0180931e011092b70080911e0190911f018b5a994208f482c08091800290918102892b09f478c080911a01882309f097c0809146010442',
    '015aa5882341f080912201909123018d5f934008f45fc080916400882309f077c08091f100882309f072c080912403882309f06dc0aa2041f4bb2031f48091190390911a03892be1f04091190350911a032e2d30e08a2d90e08b0d911d840f951f8217930738f42e2d2a192b19241b203540f404c02e2df0e5fe1528f420e503c02e2d17b7',
    '015ba4213590f48091ce029091cf028e58914038f08091cc029091cd028f50914020f413bc10934b010bc02093cc0080911e0190911f018b5a994210f010924b016091d7007091d800822f0e945e058093cc002cc080912201909123018d5f934028f521c080911b03f2cf809105018823d1f480914601882369f080912201909123010a61',
    '015ca38d5f934078f084e683bdad9880916f00ddcf80911a01882341f480912703882321f4f1cf13bc1092cc0080917700882329f080913603882309f443c013bc1092cc0010921a011092450110924401109364001092240310920401109311011092f10010920d01109205011092130110924601c098c198109247011092270310921af2',
    '015da20101109223011092220110921f0110921e01109376001093d00080913603882319f4b1e0bc8309c080e090e060ea0e94e7170c94003ce1e0ec83ff24cc2422246ac08091b4038823f9f090918c0380918e039817b9f490918d0380918f03981789f480918d0390918c03982e8824b401690f711d709383026093820283e090e0c1ef',
    '015ea10e94f4171093e4028091b5038823a1f09091900380919103981759f460919003609384021092850285e090e070e00e94f41782e08093e4028091b6038823a1f09091920380919303981759f460919203609386021092870287e090e070e00e94f41783e08093e4028091b403882341f48091b503882321f48091b603882359f04e25',
    '015fa0109376001093d0001092b4031092b5031092b60301e08091800290918102892b69f080914601882321f08091a602805e02c08091a8028093b40003c0ffeff093b40080914701882369f48091cc009091b400981710f49093cc008091cc0090e09bbd8abd8ab59bb54a9738f08ab59bb5499799bd88bd0c94e40819bc18bc0c94c052',
    '01609fe408991b79e004c0991f961708f0961b881f7a95c9f780950895aa1bbb1b51e107c0aa1fbb1fa617b70710f0a61bb70b881f991f5a95a9f780959095bc01cd010895a8e1b0e042e050e00c94fb17262fe199fecf9fbb8ebb2dbb0fb6f894e29ae19a0fbe019608950e94e717272f0c94e817dc01cb01fc01e199fecf06c0ffbb11b8',
    '01619eeebbe09a31960db20d9241505040b8f70895f894ffcf0103010301010101f80301010101013202040601040801017301017f063f3d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ff010101000205040607010300060107030204050155fc51',
    '01629d0108b239000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
]; //升级数据包


var index = 0;  //第一个数据包

Page({

    /**
     * 页面的初始数据
     * serviceId = "0000fff0-0000-1000-8000-00805f9b34fb";                                * characteristicId_TX = "0000fff1-0000-1000-8000-00805f9b34fb";  //写
     * characteristicId_RX = "0000fff2-0000-1000-8000-00805f9b34fb";  //读  
     */
    data: {
        deviceId: '',
        name: '',
        deviceId_Tx: '',    //D4:F5:13:6E:C3:C8
        //此处deviceId和characteristicId需完全匹配，字母应全部大写

        // serviceId: '0000FEE0-0000-1000-8000-00805F9B34FB',
        // characteristicId: '0000FEE1-0000-1000-8000-00805F9B34FB',
        // characteristicId_RX: '0000FEE1-0000-1000-8000-00805F9B34FB',

        serviceId: '0000FFF0-0000-1000-8000-00805F9B34FB',//特征值对应服务的uuid
        characteristicId: '0000FFF1-0000-1000-8000-00805F9B34FB', //特征值uuid写 
        characteristicId_RX: '0000FFF2-0000-1000-8000-00805F9B34FB',  //读

        CMD_OPEN: [115, 16, 16],     //开机
        CMD_CLOSE: [115, 17, 17],     //关机  
        electric: '',        //电流
        art_strike: '',      //引弧电流
        thrust: '',          //推力电流
        startUP: [115, 3, 3],   //开始升级 (73,03,03)
        endEOT: [4],   //结束数据传送，发送EOT(0X04)

        stateACK: '06',   //确认字符ACK（0x06）,对应十进制[6]
        stateNAK: '15',   //传输失败，重传NAK(0x15),对应十进制[21]
        stateCurrent: 'WAITING',  //发送状态：WAITING:SENDDING = '等待':'发送'
        stateFail: [54]    //升级失败EE57(0X36),对应十进制[54]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        this.setData({
            deviceId: options.deviceId,
            name: options.name,
            deviceId_Tx: options.deviceId
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {     
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getCharacter();  //获取特征值
        // this.receiveMsg();    //接受数据

        // 保持屏幕常亮
        wx.setKeepScreenOn({
            keepScreenOn: true
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.closeConnect();
    },

    /**
     * 断开连接
     */
    closeConnect: function (e) {
        var that = this;
        wx.stopBluetoothDevicesDiscovery({
            success: function (res) {
                console.log('停止搜索附近的蓝牙设备', res)
            }
        });

        wx.closeBLEConnection({
            deviceId: that.data.deviceId,
            success: function (res) {
                console.log('断开已连接设备', res);

                wx.showToast({
                    title: '连接已断开',
                    icon: 'success'
                });

                setTimeout(function () {
                    wx.navigateBack();
                }, 2000)
            }
        });

    },

    /**
     * switch开关机
     */
    switchChange: function (e) {
        var that = this;
        console.log('switch 发生 change 事件，携带值为', e.detail.value) //true或false
        if (e.detail.value) {    //开机
            that.openMsg();
        } else {
            that.closeMsg();    //关机
        }
    },


    /**
     * 发送数据-开机
     */
    openMsg: function () {
        var that = this;
        that.writeCharacter(that.data.CMD_OPEN);
    },

    /**
     * 关机
     */
    closeMsg: function () {
        var that = this;
        that.writeCharacter(that.data.CMD_CLOSE);
    },

    //读取服务的特征值
    getCharacter: function () {
        var that = this;

        /* services.forEach(function (value, index) {
            if (value.uuid == that.data.serviceId) {
                that.setData({
                    serviceId: value.uuid
                })
                console.log('value的值',value);
                console.log('serviceId的值', that.data.serviceId);
            }
        }); */

        wx.getBLEDeviceCharacteristics({
            deviceId: that.data.deviceId_Tx,
            // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
            serviceId: that.data.serviceId,   //蓝牙服务 uuid
            success: function (res) {
                console.log('该设备的MAC', that.data.serviceId);
                console.log('该ID设备的特征值', res);
                setTimeout(function () {
                    that.openNotifyService();
                }, 1000)
            },
            fail: function (res) {
                console.log('获取特征值失败', res);
                console.log('serviceId的值', that.data.serviceId);
                wx.showToast({
                    title: '蓝牙设备不匹配',
                });

                // wx.showModal({
                //     title: '错误信息',
                //     content: "errCode:"+res.errCode+"\n"+"errMsg:"+res.errMsg,
                // })

                setTimeout(function () {
                    wx.hideToast();
                }, 2000);
            }
        })
    },

    //发送数据给蓝牙设备--注意：必须设备的特征值支持write才可以成功调用
    writeCharacter: function (order) {
        var that = this;
        var buffer = that.hexStringToArrayBuffer(order);    //测试数据

        wx.writeBLECharacteristicValue({
            // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
            deviceId: that.data.deviceId_Tx,
            // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
            serviceId: that.data.serviceId,
            // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
            characteristicId: that.data.characteristicId,
            // 这里的value是ArrayBuffer类型
            value: buffer,
            success: function (res) {
                console.log('数据发送成功', res);
            },
            fail: function (res) {
                console.log('数据发送失败', res);
            }
        })
    },

    //将需要发送的数据转换成十六进制
    hexStringToArrayBuffer: function (str) {
        if (!str) {
            return new ArrayBuffer(0);
        }

        console.log("数据转码之前", str)

        // 要创建的 ArrayBuffer 的大小，单位为字节。
        var buffer = new ArrayBuffer(str.length);
        //ArrayBuffer 不能直接操作，而是要通过类型数组对象或 DataView 对象来操作，它们会将缓冲区中的数据表示为特定的格式，并通过这些格式来读写缓冲区的内容。
        var dataView = new DataView(buffer);

        //写入通道指令 
        for (var k = 0; k < str.length; k++) {
            dataView.setUint8(k, str[k]);
        }

        // CMD_OPEN: [115, 16, 16]
        var dataResult = [];
        for (var i = 0; i < dataView.byteLength; i++) {
            var str = dataView.getUint8(i).toString(16);
            if (dataView.getUint8(i) < 16) {
                str = '0' + str;
            }

            dataResult.push("0x" + str);
        }
        console.log("数据转码之后", dataResult);

        return buffer;
    },

    //启用低功耗蓝牙设备特征值变化时的 notify 功能，订阅特征值
    openNotifyService: function () {
        var that = this;
        wx.notifyBLECharacteristicValueChange({
            state: true, // 启用 notify 功能
            // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接  
            deviceId: that.data.deviceId,
            // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
            serviceId: that.data.serviceId,
            // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
            characteristicId: that.data.characteristicId_RX,
            success: function (res) {
                console.log('notify启动成功', res);
                that.characteristicValueChange();   //监听特征值变化
            },
            fail: function (res) {
                console.log('notify启动失败', res);
                wx.showToast({
                    title: 'notify启动失败',
                    mask: true
                });
                setTimeout(function () {
                    wx.hideToast();
                }, 2000)
            }
        })
    },

    //监听低功耗蓝牙设备的特征值变化。必须先启用notify接口才能接收到设备推送的notification
    characteristicValueChange: function () {
        var that = this;
        wx.onBLECharacteristicValueChange(function (res) {
            console.log("特征值变化", res);
            // console.log(that.ab2hext(res.value));   

            that.receiveMsg(res.value);
        });
    },

    // ArrayBuffer转16进制字符串示例
    ab2hext: function (buffer) {
        var hexArr = Array.prototype.map.call(
            new Uint8Array(buffer),
            function (bit) {
                return ('00' + bit.toString(16)).slice(-2)
            }
        )
        return hexArr.join('');
    },

    //接受数据
    receiveMsg: function (msg) {
        var that = this;
        var orderArry = that.ab2hext(msg); //16进制字符串
        // var orderArry = '731313776d4f6d0000001b28404012ff007dffff';
        // console.log(parseInt(x, 16));  //十六进制转十进制
        var data = [];
        for (var i = 0; i < orderArry.length;) {
            data.push(parseInt(orderArry.substr(i, 2), 16));
            i = i + 2;
        }

        console.log("接收蓝牙返回数据", data);
        //data=[115, 19, 19, 119, 109, 79, 109, 0, 0, 0, 27, 40, 64, 64, 18, 255, 0, 125, 255, 255]

        if (data != null && data.length > 8) {
            //电流低、高位，低、高位
            var dL1 = data[1];
            var gL1 = data[2];
            var dL2 = data[3];
            var gL2 = data[4];

            //推力重复()
            var t1 = data[5];
            var t2 = data[6];

            //引弧重复()
            var y1 = data[7];
            var y2 = data[8];
            //如果匹配
            if (dL1 == dL2 && gL1 == gL2 && t1 == t2 && y1 == y2) {
                var DL = dL1 + gL1 * 256;
                var TL = t1;
                var YH = y1;

                // currentBar.setProgress(DL - 24);
                // arcBar.setProgress(TL);
                // thrustBar.setProgress(YH);

                that.setData({
                    electric: DL,        //电流
                    art_strike: YH,      //引弧电流
                    thrust: TL           //推力电流
                });
            }
        }


        /**
         * 开始发送升级包
         */
        console.log("当前发送数据时间间隔：大包" + largeSpeed + ",小包" + speed);

        //1、NAK(0x15),接收到NAK[21]重发命令
        if (orderArry == that.data.stateNAK) {

            if (that.data.stateCurrent == 'WAITING') {
                that.setData({
                    stateCurrent: 'SENDDING'
                });

                index--;
                errorNum++; //每发送失败一次，errorNum加1
                //发送失败，重新发送
                if (errorNum == 5) {
                    largeSpeed = 500;
                } else if (errorNum > 5) {
                    largeSpeed = 1000;
                }
                setTimeout(function () {
                    that.sendPackage(index);
                }, largeSpeed)
            }

        }

        //3、收到ACK(0X06),发送协议的下一个数据包
        if (orderArry == that.data.stateACK) {
            clearInterval(timer1);  //停止发送升级命令

            largeSpeed = 200;   //大包间隔
            errorNum = 0;

            if (index < packageBox.length) {
                if (that.data.stateCurrent == 'WAITING') {
                    that.setData({
                        stateCurrent: 'SENDDING'
                    });

                    setTimeout(function () {
                        that.sendPackage(index);
                    }, largeSpeed);

                }

            } else {
                wx.hideLoading();
                if (index == packageBox.length) {
                    wx.showToast({
                        title: '升级完成',
                        success: function () {
                            // wx.navigateBack();
                        }
                    });
                    setTimeout(function () {
                        wx.hideToast();
                    }, 2000);

                    console.log('------------升级完成--------------');
                }
                index++;

                setTimeout(function () {
                    //发EOT(0X04),结束传送
                    that.writeCharacter(that.data.endEOT);
                }, 5000);

                return;
            }

        }

        //4、升级超时（暂定30分钟）显示EE57，必须重新上电
        if (orderArry == that.data.stateFail) {
            //发EE57(0X36),升级超时显示（升级失败，机器重新上电）
            that.writeCharacter(that.data.stateFail);
        }

    },

    //电流调节
    sliderChange1: function (e) {
        var that = this;
        console.log("电流调节", e);

        var current = e.detail.value;
        that.setData({
            electric: current
        });
        var currentH = 0, currentL = 0;
        if (current > 255) {//电流高低位
            currentH = current / 256;
            currentL = current - currentH * 256;
        } else {
            currentH = 0;
            currentL = current;
        }

        var order = [115, 19, 19, currentL, currentH, currentL, currentH];
        that.writeCharacter(order);

    },


    //引弧电流调节
    sliderChange2: function (e) {
        var that = this;
        console.log("引弧电流调节", e);

        var current = e.detail.value;
        that.setData({
            art_strike: current
        });
        var order = [115, 21, 21, current, current];
        that.writeCharacter(order);
    },

    //推力电流调节
    sliderChange3: function (e) {
        var that = this;
        console.log("推力电流调节", e);

        var current = e.detail.value;
        that.setData({
            thrust: current
        });
        var order = [115, 20, 20, current, current];
        that.writeCharacter(order);
    },

    //升级系统
    updataSDK: function () {
        var that = this;

        wx.showLoading({
            title: '正在下载资源包，请稍后',
            mask: true,
            success: function (res) {
                //从服务器获取数据包
                console.log('数据包个数', packageBox.length)
                console.log("------------开始升级-----------------")

                timer1 = setInterval(function () {
                    that.writeCharacter(that.data.startUP); //每隔2s发送一次
                }, 2000);
            }
        });

        // that.sendPackage(0);    //发送数据包0

    },

    //发送数据包
    sendPackage: function (num) {
        var that = this;
        var data2 = [], orderBox = [], dataBefore = [];

        if (index < packageBox.length) {
            wx.showLoading({
                title: '设备升级中...' + num,
                mask: true,
                success: function () {
                    index++;
                }
            });

            for (var j = 0; j < packageBox[num].length;) {
                dataBefore.push("0x" + packageBox[num].substr(j, 2));  //共133个字节

                data2.push(parseInt(packageBox[num].substr(j, 2), 16));  //10进制
                j = j + 2;
            }

            for (var k = 0; k < data2.length;) {
                var orderList = data2.slice(k, k + 20); //每20个字节组成一个新数据包
                orderBox.push(orderList);
                k = k + 20;
            }
            console.log("发送协议数据包" + num, dataBefore);
            console.log("发送协议数据包" + num, orderBox);

            // 此处通过定时器延迟发送数据
            var item = 0;
            var timer2 = setInterval(function () {
                if (item < orderBox.length) {
                    that.writeCharacter(orderBox[item]);

                    if (item == orderBox.length - 1) {
                        that.setData({
                            stateCurrent: 'WAITING'
                        });

                        console.log('当前发送状态', that.data.stateCurrent);
                    } else {
                        that.setData({
                            stateCurrent: 'SENDDING'
                        });
                    }

                    item++;

                } else {
                    clearInterval(timer2);
                    item = 0;
                }
            }, speed)

        } else {
            clearInterval(timer1);
            wx.hideLoading();
        }

    }


})